package com.anolesoft.twelveFactory.planManagement.service.impl;

import com.anolesoft.twelveFactory.planManagement.entity.ProcessingHistory;
import com.anolesoft.twelveFactory.planManagement.mapper.ProcessingHistoryMapper;
import com.anolesoft.twelveFactory.planManagement.service.ProcessingHistoryService;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProcessingHistoryServiceImpl implements ProcessingHistoryService {
    @Resource
    private ProcessingHistoryMapper processingHistoryMapper;

    @Override
    public PageData<List<ProcessingHistory>> listProcessingHistory(ProcessingHistory processingHistory) {
        Page<ProcessingHistory> page = new Page<ProcessingHistory>(processingHistory.getPageNo(), processingHistory.getPageSize());
        List<ProcessingHistory> listProcessingHistory=processingHistoryMapper.listProcessingHistory(processingHistory,page);
        //        序号序号序号!!!!
        // 父图号为空
        List<ProcessingHistory> roots = processingHistoryMapper.listProcessingHistory1();

        List<ProcessingHistory> processingHistories=new ArrayList<>();
        for (int i = 0; i <listProcessingHistory.size(); i++) {
            if(listProcessingHistory.get(i).getWyzd()!=null){
                ProcessingHistory p=this.processingHistoryMapper.selectByWYZD(listProcessingHistory.get(i).getWyzd());
            while (1 == 1) {
                if(p!=null){  processingHistories.add(p);}
                if (p.getFth() == null || p==null)
                    break;
                p = this.processingHistoryMapper.selectByTh(p.getFth());
            }
            }
        }
//        过滤
//        List<ProcessingHistory> planIssueds1=processingHistories.stream().distinct().collect(Collectors.toList());
//        得到id
        List<String> id=processingHistories.stream().map(ProcessingHistory::getObjuid).collect(Collectors.toList());


        roots = roots.stream().filter(l -> {
            return  id.contains(l.getObjuid());
        }).collect(Collectors.toList());

        roots.forEach(root -> {
            ProcessingHistory.setChildren(processingHistories,root);
        });

//        sort() 方法用于对数组的元素进行排序。
        roots.sort((e1, e2) -> {
            return -e1.compareTo(e2);
        });

        for (int i = 0; i < roots.size(); i++) {
//            码  .前
            String code = String.valueOf(i+1);
            roots.get(i).setCode(code);
            List<ProcessingHistory> children = roots.get(i).getChildren();
            if(children != null && !children.isEmpty()) {
                //            码  .前+后
                ProcessingHistory.setChildrenCode(children, code);
            }
        }
        return new PageData<>(((int) page.getTotal()),roots);
  
    }

    @Override
    public Integer insert(ProcessingHistory processingHistory) {
//        Employee employee
        return this.processingHistoryMapper.insert(processingHistory);
    }

    @Override
    public Integer delete(String objuid) {
        return this.processingHistoryMapper.delete(objuid);
    }

    @Override
    public Integer update(ProcessingHistory processingHistory) {
        return this.processingHistoryMapper.update(processingHistory);
    }

    @Override
    public ProcessingHistory selectById(String objuid) {
        return this.processingHistoryMapper.selectById(objuid);
    }
}
